package com.jp.knowledge.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.google.gson.JsonObject;
import com.jp.knowledge.R;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.HeadLineData;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.util.ResultSubscriber;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/2/16.
 */

public class SearchRecommentAdapter extends RecycleVAdapter<HeadLineData> implements View.OnClickListener, ResultSubscriber.OnResultListener {

    private SimpleDateFormat dateFormat;
    private int type;

    public SearchRecommentAdapter(Context context, @NonNull List<HeadLineData> list){
        this(context, list, 11);
    }

    public SearchRecommentAdapter(Context context, @NonNull List<HeadLineData> list, int type) {
        super(context, list);
        this.dateFormat = new SimpleDateFormat("MM月dd日");
        this.type = type;
    }

    public void setType(int type){
        this.type = type;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.fragment_search_recommend;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        HeadLineData data = getData(position);

//        if(this.type == 21){            // 行业大观察
//            holder.get(R.id.search_recomend_title_icon_view).setVisibility(View.VISIBLE);
//            holder.get(R.id.search_recomend_title_icon).setVisibility(View.GONE);
//            holder.get(R.id.search_recomend_title_flag).setVisibility(View.VISIBLE);
//            holder.get(R.id.search_recomend_desc).setVisibility(View.VISIBLE);
//            holder.setTextView(R.id.search_recomend_title_flag, data.getTag());
//            setTextViewText(holder.getTextView(R.id.search_recomend_desc), data.getDesc());
//            holder.get(R.id.search_recomend_collection).setVisibility(View.GONE);
//        } else if(this.type == 17){     // 每日评说
//            holder.get(R.id.search_recomend_title_icon_view).setVisibility(View.VISIBLE);
//            holder.get(R.id.search_recomend_title_icon).setVisibility(View.VISIBLE);
//            holder.get(R.id.search_recomend_title_flag).setVisibility(View.GONE);
//            holder.get(R.id.search_recomend_desc).setVisibility(View.VISIBLE);
//            holder.setImageUrl(R.id.search_recomend_title_icon, data.getIcon());
//            setTextViewText(holder.getTextView(R.id.search_recomend_desc), data.getDesc());
//        } else if(this.type == 11){     // 全民推荐
//            holder.get(R.id.search_recomend_title_icon_view).setVisibility(View.GONE);
//            holder.get(R.id.search_recomend_desc).setVisibility(View.GONE);
//        }

        if((data.getIcon() == null || data.getIcon().trim().length() == 0)
                && (data.getTag() == null || data.getTag().trim().length() == 0)) {
            holder.get(R.id.search_recomend_title_icon_view).setVisibility(View.GONE);
        } else{
            holder.get(R.id.search_recomend_title_icon_view).setVisibility(View.VISIBLE);
            if(data.getTag() != null && data.getTag().trim().length() > 0){
                holder.get(R.id.search_recomend_title_flag).setVisibility(View.VISIBLE);
                setTextViewText(holder.getTextView(R.id.search_recomend_title_flag), data.getTag());
            } else{
                holder.get(R.id.search_recomend_title_flag).setVisibility(View.GONE);
            }
            if(data.getIcon() != null && data.getIcon().trim().length() > 0){
                holder.get(R.id.search_recomend_title_icon).setVisibility(View.VISIBLE);
                holder.setImageUrl(R.id.search_recomend_title_icon, data.getIcon());
            } else{
                holder.get(R.id.search_recomend_title_icon).setVisibility(View.GONE);
            }
        }
        setTextViewText(holder.getTextView(R.id.search_recomend_desc), data.getDesc());
        if(this.type == 11){     // 全民推荐
            holder.get(R.id.search_recomend_title_icon_view).setVisibility(View.GONE);
            holder.get(R.id.search_recomend_desc).setVisibility(View.GONE);
        } else if(this.type == 21){
            holder.get(R.id.search_recomend_collection).setVisibility(View.GONE);
        }

        holder.setTextView(R.id.search_recomend_title, data.getTitle());
        holder.setTextView(R.id.search_recomend_time, dateFormat.format(data.getCreatetime()));
        try {
            String imgPath;
            if (type == 21 || type == 17)
                imgPath = data.getCover().replaceAll("\n", "").replaceAll(" ", "");
            else
                imgPath = data.getIcon().replaceAll("\n", "").replaceAll(" ", "");
            final ImageView image = holder.getImageView(R.id.search_recomend_image);
            image.setScaleType(ImageView.ScaleType.FIT_CENTER);
            image.setImageResource(R.mipmap.morentutouxiang);
            Glide.with(mContext).load(imgPath).placeholder(R.mipmap.morentutouxiang).error(R.mipmap.morentutouxiang).listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    image.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    return false;
                }
                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    return false;
                }
            }).into(image);
        } catch (Exception e){}

        TextView collectionBtn = holder.getTextView(R.id.search_recomend_collection);
        if (data.getIsFollow() == 1) {
            collectionBtn.setText("已收藏");
            collectionBtn.setTextColor(mContext.getResources().getColor(R.color.font_gray_think));
            collectionBtn.setBackground(mContext.getResources().getDrawable(R.drawable.search_btn_gray));
        } else {
            collectionBtn.setText("收藏");
            collectionBtn.setTextColor(mContext.getResources().getColor(R.color.theme_color));
            collectionBtn.setBackground(mContext.getResources().getDrawable(R.drawable.search_btn_blue));
        }
        collectionBtn.setTag(position);
        collectionBtn.setOnClickListener(this);
    }

    private void setTextViewText(TextView textView, String text){
        if(text == null || text.trim().length() == 0){
            textView.setVisibility(View.GONE);
        } else{
            textView.setVisibility(View.VISIBLE);
            textView.setText(text);
        }
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        HeadLineData data = getData(position);

        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("id", data.getId());
        param.addProperty("type", /*data.getType()*/type);
        if(data.getIsFollow() == 1)
            HttpHelper.getInstance(mContext).unCollection(param, (int) v.getTag(), this);
        else
            HttpHelper.getInstance(mContext).collectionHead(param, (int) v.getTag(), this);

        data.setIsFollow(data.getIsFollow()==1 ? 0 : 1);
//        notifyDataSetChanged();
        notifyItemChanged((int) v.getTag());
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
    }
    @Override
    public void onError(int requestType) {
        HeadLineData headLineData = getData(requestType);
        headLineData.setIsFollow(headLineData.getIsFollow()==1 ? 0 : 1);
//        notifyDataSetChanged();
        notifyItemChanged(requestType);

        if(this.collectionCallback != null)
            this.collectionCallback.onCollection(requestType, headLineData.getId(), headLineData.getIsFollow()==1, false);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0) {
            onError(requestType);
            return;
        } else if(this.collectionCallback != null) {
            HeadLineData headLineData = getData(requestType);
            this.collectionCallback.onCollection(requestType, headLineData.getId(), headLineData.getIsFollow() == 1, true);
        }
    }

    private CollectionCallback collectionCallback;
    public void setCollectionCallback(CollectionCallback collectionCallback){
        this.collectionCallback = collectionCallback;
    }
    public interface CollectionCallback{
        void onCollection(int position, String id, boolean isCollection, boolean isSuccess);
    }
}
